import Vue from 'vue'
import Router from 'vue-router'
import { getMenuProjectList } from '@/api/project'

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: 'home',
      name: 'Home',
      meta: { title: '首页', icon: 'home' },
      component: () => import('@/views/Home')
    }]
  },
  {
    path: '/projectIndex',
    component: Layout,
    name: 'ProjectIndex',
    meta: { title: '项目管理', icon: 'project-manage' },
    children: [{
      path: 'oneKeyPack',
      name: 'oneKeyPack',
      component: () => import('@/views/projectManage/OneKeyPack/index'),
      meta: { title: '一键打包', icon: 'one-key-pack' },
      children: []
    }, {
      path: 'projectList',
      name: 'projectList',
      component: () => import('@/views/projectManage/projectList/index.vue'),
      meta: { title: '项目列表', icon: 'project' },
      children: []
    }]
  },
  {
    path: '/toolsIntegration',
    component: Layout,
    name: 'ToolsIntegration',
    meta: { title: '工具集', icon: 'tools-integration' },
    children: [{
      path: 'imgMigration',
      name: 'imgMigration',
      component: () => import('@/views/toolsIntegration/imgMigration/index.vue'),
      meta: { title: '镜像迁移', icon: 'img-migration' },
      children: []
    }]
  },
  {
    path: '/systemSetting',
    component: Layout,
    name: 'SystemSetting',
    meta: { title: '系统设置', icon: 'system-setting' },
    children: []
  },
  {
    path: '/personView',
    component: Layout,
    children: [{
      path: 'person',
      name: 'Person',
      meta: { title: '个人信息', icon: 'person' },
      component: () => import('@/views/person/index.vue')
    }]
  }
]

export const systemRoutes = [{
  path: '/systemSetting',
  component: Layout,
  name: 'SystemSetting',
  meta: { title: '系统设置', icon: 'system-setting' },
  children: [{
    path: 'userManagement',
    name: 'userManagement',
    component: () => import('@/views/systemSetting/index'),
    meta: { title: '用户管理', icon: 'user-manage', roles: ['admin', 'ops', 'builder'] },
    children: [{
      path: 'systemAccount',
      name: 'systemAccount',
      component: () => import('@/views/systemSetting/usermanagement/systemaccount/index'),
      meta: { title: '系统账户', icon: 'system-account', roles: ['admin'] },
      children: []
    }, {
      path: 'gitProvider',
      name: 'gitProvider',
      component: () => import('@/views/systemSetting/usermanagement/gitprovider/index'),
      meta: { title: 'Git凭证', icon: 'git-provider', roles: ['admin', 'ops', 'builder'] },
      children: []
    }]
  }, {
    path: 'systemConfig',
    name: 'systemConfig',
    component: () => import('@/views/systemSetting/configs/index'),
    meta: { title: '系统配置', icon: 'system-config', roles: ['admin', 'ops'] },
    children: []
  }]
}]

// 创建路由实例
const createRouter = () => new Router({
  routes: constantRoutes
})

const router = createRouter()

router.selfAddRoutes = function(routerConfig) {
  router.matcher = new Router().matcher
  router.addRoutes(routerConfig)
}

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重置路由
}

export default router
